﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessSysHistory : IBusinessSysHistory
    {
        IDALSysHistory _dalHistory;
        IUnitOfWork _unitWork;
        public BusinessSysHistory(IDALSysHistory dalHistory, IUnitOfWork unitWork)
        {
            _dalHistory = dalHistory;
            _unitWork = unitWork;
        }
        public void Add(SysHistoryModel model)
        {
            var dataEntity = Mapper.Map<Tbl_SysHistory>(model);
            _dalHistory.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(SysHistoryModel model)
        {
            var dataEntity = Mapper.Map<Tbl_SysHistory>(model);
            _dalHistory.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<SysHistoryModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_SysHistory>>(models);
            _dalHistory.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<SysHistoryModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_SysHistory>>(models);
            _dalHistory.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<SysHistoryModel> GetAll(bool isTracking = true)
        {
            var entities = _dalHistory.GetAll(isTracking);
            return Mapper.Map<List<SysHistoryModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalHistory.Delete(primaryKey);
            _unitWork.Commit();
        }

        public SysHistoryModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalHistory.Get(primaryKey);
            return Mapper.Map<SysHistoryModel>(dataEntity);
        }

        public void Delete(SysHistoryModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_SysHistory>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalHistory.Delete(where);
            _unitWork.Commit();
        }

        public SysHistoryModel Get(SysHistoryModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_SysHistory>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            var dataEntity = _dalHistory.Get(where);
            return Mapper.Map<SysHistoryModel>(dataEntity);
        }

        public List<SysHistoryModel> GetAll(SysHistoryModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_SysHistory>();
            var entities = _dalHistory.GetAll(where, isTracking);
            return Mapper.Map<List<SysHistoryModel>>(entities);
        }


        public List<SysHistoryModel> GetAll(SysHistoryModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
